package com.hcf.admin.controller;

import com.hcf.admin.common.convention.result.Result;
import com.hcf.admin.common.convention.result.ResultTool;
import com.hcf.admin.dto.req.ShortLinkGroupSortReqDto;
import com.hcf.admin.dto.req.ShortLinkGroupUpateReqDto;
import com.hcf.admin.dto.req.ShortlinkGroupSaveReqDto;
import com.hcf.admin.dto.resp.GroupRespDto;
import com.hcf.admin.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/shortlink/admin/v1")
public class GroupController {

    @Autowired
    private GroupService groupService;

    /**
     * 新增短链接分组
     */
    @PostMapping("/group")
    public Result<Void> save(@RequestBody ShortlinkGroupSaveReqDto shortlinkGroupSaveReqDto) {
        groupService.saveGroup(shortlinkGroupSaveReqDto);
        return ResultTool.success();
    }


    /**
     * 查询短链接分组
     */
    @GetMapping("/group")
    public Result<List<GroupRespDto>> listGroup(){
        List<GroupRespDto> groupRespDtoList = groupService.listGroup();
        return ResultTool.success(groupRespDtoList);
    }

    /**
     * 修改短链接分组
     */
    @PutMapping("/group")
    public Result updateGroup(@RequestBody ShortLinkGroupUpateReqDto groupUpateReqDto){
        if(groupService.updateGroup(groupUpateReqDto)){
            return ResultTool.success();
        }else{
            return ResultTool.failure(null, "分组修改失败");
        }
    }


    /**
     * 删除短链接分组
     */
    @DeleteMapping("/group")
    public Result deleteGroup(@RequestParam String id){
        if(groupService.deleteGroup(id)){
            return ResultTool.success();
        }else {
            return ResultTool.failure(null, "分组删除失败");
        }
    }


    /**
     * 短链接分组排序
     */
    @PostMapping("/group/sort")
    public Result sortGroup(@RequestBody ShortLinkGroupSortReqDto groupSortReqDto){
        groupService.sortGroup(groupSortReqDto);
        return ResultTool.success();
    }

}
